import enquireJs from 'enquire.js'

export function isDef(v) {
  return v !== undefined && v !== null
}

/**
 * Remove an item from an array.
 */
export function remove(arr, item) {
  if (arr.length) {
    const index = arr.indexOf(item)
    if (index > -1) {
      return arr.splice(index, 1)
    }
  }
}

const _toString = Object.prototype.toString

export function isRegExp(v) {
  return _toString.call(v) === '[object RegExp]'
}

export function enquireScreen(call) {
  const handler = {
    match: function() {
      call && call(true)
    },
    unmatch: function() {
      call && call(false)
    }
  }
  enquireJs.register('only screen and (max-width: 767.99px)', handler)
}

export function formatSize(val) {
  if (isNaN(val)) return ""
  var list = ["KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
  // var list = ["BYTE", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
  var temp = Math.floor(Math.log(val) / Math.log(2))
  if (temp < 1) temp = 0
  var num = Math.floor(temp / 10)
  val = val / Math.pow(2, 10 * num)
  if (val.toString().length > val.toFixed(2).toString().length) val = val.toFixed(2)
  return val + "" + list[num]
}

export function download(url, name, suffix) {
  const oReq = new XMLHttpRequest()
  oReq.open('GET', url, true)
  oReq.responseType = 'arraybuffer'
  oReq.onload = function() {
    if (oReq.status === 200 || oReq.status === 0) {
      const arrayBuffer = oReq.response
      const byteArray = new Uint8Array(arrayBuffer)
      const aLink = window.parent.document.createElement('a')
      const body = window.parent.document.querySelector("body")
      const blob = new Blob([byteArray], {
        type: 'application/octet-stream'
      })
      aLink.download = name + "." + suffix
      aLink.href = URL.createObjectURL(blob)
      aLink.display = 'none'
      aLink.target = '_blank'
      body.appendChild(aLink)
      aLink.click()
      body.removeChild(aLink)
      window.URL.revokeObjectURL(aLink.href)
    } else {
      this.$message.error('下载失败')
    }
  }
  oReq.send()
}

export function base64ToFile(base64, fileName) {
  let arr = base64.split(',')
  let mime = arr[0].match(/:(.*?);/)[1]
  let bytes = atob(arr[1])
  let n = bytes.length
  let ia = new Uint8Array(n)
  while (n--) {
    ia[n] = bytes.charCodeAt(n)
  }
  return new File([ia], fileName, {
    type: mime
  })
}

export function timeFormatSecond(time) {
  var seconds = 0
  var timeArray = time.split(':')
  seconds = (+timeArray[0]) * 60 * 60 + (+timeArray[1]) * 60 + (+timeArray[2])
  return seconds
}

export function secondFormatTime(second) {
  if (second < 60) {
    if (second < 10) {
      return "00:00:0" + second
    } else {
      return "00:00:" + second
    }
  } else {
    var minuteTotal = Math.floor(second / 60)
    second = Math.floor(second % 60)
    if (minuteTotal < 60) {
      if (minuteTotal < 10) {
        if (second < 10) {
          return "00:0" + minuteTotal + ":0" + second
        } else {
          return "00:0" + minuteTotal + ":" + second
        }
      } else {
        if (second < 10) {
          return "00:" + minuteTotal + ":0" + second
        } else {
          return "00:" + minuteTotal + ":" + second
        }
      }
    } else {
      var hourTotal = Math.floor(minuteTotal / 60)
      if (hourTotal < 10) {
        hourTotal = "0" + hourTotal
      }
      var minute = Math.floor(minuteTotal % 60)
      if (minute < 10) {
        minute = "0" + minute
      }
      if (second < 10) {
        second = "0" + second
      }
      return hourTotal + ":" + minute + ":" + second
    }
  }
}
